﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using OConf.API.hub;
using OConf.IBLL;
using OConf.Model;
using OConf.Model.Common;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OConf.API.Controllers
{
    [Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class VersionController : ControllerBase
    {
        private readonly IVersionsBLL _versionsBLL;
        private readonly IHubContext<ChatHub> _hubContext;
        public VersionController(IVersionsBLL versionsBLL, IHubContext<ChatHub> hubContext)
        {
            _versionsBLL = versionsBLL;
            _hubContext = hubContext;
        }

        /// <summary>
        /// 获取命名空间下的版本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ReturnData<List<VersionsModel>> GetVersions([FromQuery] GetVersionsInput input)
        {
            return _versionsBLL.GetVersions(input);
        }

        /// <summary>
        /// 版本回滚
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ReturnData<bool>> VersionRollBackAsync([FromBody] VersionRollBackInput input)
        {
            var result = _versionsBLL.VersionRollBack(input);

            if (result.Code == 200 && result.Data != null)
            {
                await _hubContext.Clients.User(result.Data.ProjectName).SendAsync("ReceiveConfig", result.Data);

                return ReturnDataHelper<bool>.Success("操作成功", true);
            }
            else
            {
                return ReturnDataHelper<bool>.DiyResult(result.Code, result.Msg, false);
            }
        }

        /// <summary>
        /// 获取配置历史信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ReturnData<List<ConfigInfoHistoryModel>> GetConfigInfoHistory([FromQuery] GetConfigInfoHistoryInput input)
        {
            return _versionsBLL.GetConfigInfoHistory(input);
        }
    }
}
